#
# Makefile for Frost OS
#
# Compiler: i586-elf-gcc/i586-elf-g++ (4.4.0)
# Linker: i586-elf-ld (2.19.1)
# Assembler: nasm (2.07)
#
# This is a list of all non-source files that are part of the distribution.
AUXFILES := Makefile Readme.txt
UNAME := $(shell uname)
ifeq ($(UNAME), Darwin)
CC=/usr/cross/bin/i586-elf-gcc
CXX=/usr/cross/bin/i586-elf-gcc
LD=/usr/cross/bin/i586-elf-ld
QEMU=/usr/local/bin/qemu
ISOMAKER=/opt/schily/bin/mkisofs
endif
ifeq ($(UNAME), Linux)
CC=i586-elf-gcc
CXX=i586-elf-g++
LD=i586-elf-ld
QEMU=/usr/local/bin/qemu
ISOMAKER=genisoimage
endif
PROJDIRS := src include
SRCFILES := $(shell find $(PROJDIRS) -mindepth 0 -maxdepth 100 -name "*.c")
SRCFILESCPP := $(shell find $(PROJDIRS) -mindepth 0 -maxdepth 100 -name "*.cpp")
HDRFILES := $(shell find $(PROJDIRS) -mindepth 0 -maxdepth 100 -name "*.h") $(shell find $(PROJDIRS) -mindepth 0 -maxdepth 100 -name "*.inc")
FOLDERDIRECTORYINCLUDES = -I ./include -I ./include/SYSLib -I ./include/Kernel -I ./include/Loader -I ./include/SYSLib/IOStream
NASM=nasm
CFLAGS=-Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs -fno-builtin -fno-exceptions -ffreestanding
CXXFLAG=-Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs -fno-builtin -fno-rtti -fno-exceptions -ffreestanding
NFLAGS=-f elf
OBJECTS=src/Loader/loader.o src/Kernel/pagingASM.o $(SRCFILESCPP:.cpp=.o) $(SRCFILES:.c=.o)
ALLFILES = $(HDRFILES) $(SRCFILESCPP) $(SRCFILES)

all:
	make clean
	if test ! -e /usr/local/bin/qemu; then ./installQemu.sh; fi
	if test ! -e /opt/schily/bin/mkisofs; then ./installMKisofs.sh; fi
	if test ! -e /usr/bin/nasm; then if test ! -e /usr/local/bin/nasm; then ./installNASM.sh; fi; fi
	if test -e /usr/cross/bin/i586-elf-g++; \
	then make Setup; \
	else ./installGCC.sh; \
	fi
	
Setup:
ifeq ($(strip $(wildcard build)),)
	@mkdir build
endif
ifeq ($(strip $(wildcard isofiles)),)
	@mkdir isofiles
endif
	@make FrostOS.iso

FrostOS.iso: build/kernel.elf
	@cp -R grub/boot isofiles
	@cp -R grub/boot.catalog isofiles
	@cp build/kernel.elf isofiles/boot/kernel.elf
	@$(ISOMAKER) -R -b boot/grub/grub_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o FrostOS.iso isofiles

build/kernel.elf: $(OBJECTS)
	@$(LD) -T src/linker.ld -o build/kernel.elf $(OBJECTS)

%.o: %.c Makefile
	@$(CC) $(CFLAGS) $(FOLDERDIRECTORYINCLUDES) -c $< -o $@

%.o: %.cpp Makefile
	@$(CXX) $(CXXFLAG) $(FOLDERDIRECTORYINCLUDES) -c $< -o $@

src/Loader/loader.o: src/Loader/loader.s Makefile
	@$(NASM) $(NFLAGS) -o src/Loader/loader.o src/Loader/loader.s

src/Kernel/pagingASM.o: src/Kernel/pagingASM.s Makefile
	@$(NASM) $(NFLAGS) -o src/Kernel/pagingASM.o src/Kernel/pagingASM.s

todolist:
	@for file in $(ALLFILES); do fgrep -H -e TODO -e FIXME $$file; done; true

clean:
	@$(RM) -f -d -r $(wildcard $(OBJECTS) $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 100 -name "*.d") FrostOS.iso build isofiles)

run:
	$(QEMU) -cdrom FrostOS.iso

debugRun:
	@$(QEMU) -s -S -cdrom FrostOS.iso
	
gdb:
	@$(shell echo "target remote localhost:1234\nsymbol-file build/kernel.elf" > gdbCommands.txt)
	@i586-elf-gdb --command=gdbCommands.txt

dist:
	@make
	@tar --exclude=".*" -pvczf FrostOSBuild.tar.gz FrostOS.iso
	@make clean
	@tar --exclude=".*" -pvczf FrostOSSource.tar.gz ./
